#include<iostream>
#include<algorithm>
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
 
class Solution {
private:
    void postOrder(TreeNode* root, int& sum, int& res) {
        if (root == NULL) return;
        int sum1 = 0, sum2 = 0;
        postOrder(root->left, sum1, res);
        postOrder(root->right, sum2, res);
        res += abs(sum2 - sum1);
        sum = sum1 + sum2 + root->val;
    }


public:
    int findTilt(TreeNode* root) {
        int sum = 0,res = 0;
        postOrder(root, sum, res);
        return res;
    }
};